package com.drx.controller;

import com.drx.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController {
    @Autowired
    LoginService loginService;

    @RequestMapping(value = "/studentLogin",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,String> studentLogin(Integer studentNum, String password, HttpSession session){
        //写一个用于学生登录的方法，要求传入学生的学好studentNum和密码password。用于判断表中是否有该学员。
        //返回一个Map集合，里面包含如下几个值：loginResult登录结果，errorReason错误原因
        Map<String,String> result=new HashMap<String, String>();
        //调用service层的查询方法，查找改用户
        result=loginService.verifyStudent(studentNum,password);
        if ("true".equals(result.get("loginResult"))){
            //登陆成功以后，将学号放入到session中
            session.setAttribute("studentNum",studentNum);
        }
        return result;
    }

    @RequestMapping(value = "/administorLogin",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,String> administorLogin(String administorName, String password, HttpSession session){
        //写一个用于管理员登录的方法，要求传入学生的学好studentNum和密码password。用于判断表中是否有该学员。
        //返回一个Map集合，里面包含如下几个值：loginResult登录结果，errorReason错误原因
        Map<String,String> result=new HashMap<String, String>();
        //调用service层的查询方法，查找改用户
        result=loginService.verifyAdministor(administorName,password);
        if ("true".equals(result.get("loginResult"))){
            //登陆成功以后，将学号放入到session中
            session.setAttribute("administorName",administorName);
        }
        return result;
    }

    //定义一个退出登录的方法
    @RequestMapping(value = "/exitLogin",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String ,String > exitLogin(HttpSession session){
        Map<String,String> result=new HashMap<String, String>();
        //尝试从session中获取到studentNum
        Integer studentNum = (Integer) session.getAttribute("studentNum");
        if (studentNum==null){
            result.put("exitLoginResult","false");
            result.put("errorReason","您还未登录");
        }else{
            //用户已经登录，去掉session中保存的studentNum
            session.setAttribute("studentNum",null);
            result.put("exitLoginResult","true");
        }
        return result;
    }

    //定义一个管理员退出登录的方法
    @RequestMapping(value = "/administorExitLogin",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String ,String > administorExitLogin(HttpSession session){
        Map<String,String> result=new HashMap<String, String>();
        //尝试从session中获取到studentNum
        String  administorName = (String) session.getAttribute("administorName");
        if (administorName==null){
            result.put("exitLoginResult","false");
            result.put("errorReason","您还未登录");
        }else{
            //用户已经登录，去掉session中保存的studentNum
            session.setAttribute("administorName",null);
            result.put("exitLoginResult","true");
        }
        return result;
    }
}
